<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Power extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
	}

	public function index()
	{
		$this->view('power');
	}

	public function data()
	{
		$page = ($this->input->post('page') ? intval($this->input->post('page')) : 1);
		$rows = ($this->input->post('rows') ? intval($this->input->post('rows')) : 10);
		$result['total'] = $this->db->count_all_results('power');
		$start = ($page - 1) * $rows;
		$result['rows'] = $this->db->limit($rows, $start)->get('power')->result_array();
		echo json_encode($result);
	}

	public function add()
	{
		$d['admin_ower'] = $this->power;
		$this->load->view('power_add', $d);
	}

	public function insert()
	{
		$name = $this->input->post('name');
		$actions = $this->input->post('actions');
		$data = array('name' => $name, 'actions' => implode(',', $actions));
		$rs = $this->db->insert('power', $data);

		if ($rs) {
			echo json_encode(array('msg' => '添加成功', 'error' => 0));
		}
		else {
			echo json_encode(array('msg' => '添加失败', 'error' => 1));
		}
	}

	public function edit($id)
	{
		$id = intval($id);
		$this->db->where('id', $id);
		$rs = $this->db->get('power');
		$d = $rs->row_array();
		$d['admin_ower'] = $this->power;
		$this->load->view('power_edit', $d);
	}

	public function update()
	{
		$name = $this->input->post('name');
		$actions = $this->input->post('actions');
		$data = array('name' => $name, 'actions' => implode(',', $actions));
		$id = intval($this->input->post('id'));
		$rs = $this->db->where('id', $id)->update('power', $data);

		if ($rs) {
			echo json_encode(array('msg' => '修改成功', 'error' => 0));
		}
		else {
			echo json_encode(array('msg' => '修改失败', 'error' => 1));
		}
	}

	public function del()
	{
		$id = $this->input->get('id');

		if ($id) {
			$rs = $this->db->where_in('id', $id)->delete('power');

			if ($rs) {
				echo json_encode(array('msg' => '删除成功', 'error' => 0));
			}
			else {
				echo json_encode(array('msg' => '删除失败', 'error' => 1));
			}
		}
		else {
			echo json_encode(array('msg' => '删除失败！至少选择一行数据', 'error' => 0));
		}
	}
}

?>
